package com.islewars.client.lobby;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.islewars.client.ClientStaticData;
import com.islewars.client.mapEditor.MapEditor;
import com.islewars.client.service.GameService;
import com.islewars.client.service.GameServiceAsync;

public class Lobby {
	private static GameServiceAsync gameServiceAsync = GWT.create(GameService.class);

	public Lobby() {
		init();
	}

	private void init() {
		HorizontalPanel mainHorizontalPanel = new HorizontalPanel();
		mainHorizontalPanel.setWidth("80%");
		mainHorizontalPanel.setHeight(Window.getClientHeight() + "px");
		((HorizontalPanel)mainHorizontalPanel).setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
//		((HorizontalPanel)mainHorizontalPanel).setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);

		VerticalPanel playersVerticalPanel = new VerticalPanel();
		
		playersVerticalPanel.add(new Label("Players"));
		
		final FlexTable playersGrid = new FlexTable();
		playersGrid.setWidth("100px");
		playersVerticalPanel.add(playersGrid);
		mainHorizontalPanel.add(playersVerticalPanel);
		
		VerticalPanel middleVerticalPanel = new VerticalPanel();
		
		middleVerticalPanel.add(new Label("Games"));

		final FlexTable gamesGrid = new FlexTable();
		middleVerticalPanel.add(gamesGrid);

		HorizontalPanel chatHorizontalPanel = new HorizontalPanel();
		final TextBox chatTextBox = new TextBox();
		chatHorizontalPanel.add(new Label("Say:"));
		chatHorizontalPanel.add(chatTextBox);
		Button sayButton = new Button("Say");
		sayButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				chatTextBox.setText("");
				
				gameServiceAsync.say(ClientStaticData.player.name, chatTextBox.getValue(), new AsyncCallback<Void>() {
					@Override
					public void onSuccess(Void result) {
					}
					
					@Override
					public void onFailure(Throwable caught) {
						caught.printStackTrace();
					}
				});
			}
		});
		chatHorizontalPanel.add(sayButton);
		middleVerticalPanel.add(chatHorizontalPanel);
		
		final FlexTable chatGrid = new FlexTable();
		middleVerticalPanel.add(chatGrid);
		
		Timer lobbyTimer = new LobbyTimer(playersGrid, chatGrid, gamesGrid);
		lobbyTimer.scheduleRepeating(1000);
		
		mainHorizontalPanel.add(middleVerticalPanel);
		
		VerticalPanel rightVerticalPanel = new VerticalPanel();
		
		Button createGameButton = new Button("Create game");
		createGameButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				new MapEditor();
			}
		});
		rightVerticalPanel.add(createGameButton);
		
		mainHorizontalPanel.add(rightVerticalPanel);
		
		RootPanel.get().setStyleName("rootStyle");
		RootPanel.get().clear();
		RootPanel.get().add(mainHorizontalPanel);
	}
}
